﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Dropthings.DAL;

namespace Dropthings.Business.Facade
{
    partial class Facade
    {
        public DataSet GetAmmeterMonthDatas(string identifyCode, int year, string energyTypeNo = "01")
        {
            if (energyTypeNo == "01")
            {
                UQ_GetAmmeterMonthDatas query = new UQ_GetAmmeterMonthDatas(identifyCode, year, energyTypeNo);
                return query.ExecuteDataset();
            }
            //else if (energyTypeNo == "02")
            //{
            //    UQ_GetWaterMonthDatas query = new UQ_GetWaterMonthDatas(identifyCode, year, energyTypeNo);
            //    return query.ExecuteDataset();
            //}
            else if (energyTypeNo == "03")
            {
                UQ_GetGasMonthDatas query = new UQ_GetGasMonthDatas(identifyCode, year, energyTypeNo);
                return query.ExecuteDataset();
            }
            //else if (energyTypeNo == "16")
            //{
            //    UQ_GetHeatMonthDatas query = new UQ_GetHeatMonthDatas(identifyCode, year, energyTypeNo);
            //    return query.ExecuteDataset();
            //}
            return null;
        }

        public DataSet GetAmmeterDayDatas(string identifyCode, int year, int month, string energyTypeNo = "01")
        {
            if (energyTypeNo == "01")
            {
                UQ_GetAmmeterDayDatas query = new UQ_GetAmmeterDayDatas(identifyCode, year, month, energyTypeNo);
                return query.ExecuteDataset();
            }
            //else if (energyTypeNo == "02")
            //{
            //    UQ_GetWaterDayDatas query = new UQ_GetWaterDayDatas(identifyCode, year, month, energyTypeNo);
            //    return query.ExecuteDataset();
            //}
            else if (energyTypeNo == "03")
            {
                UQ_GetGasDayDatas query = new UQ_GetGasDayDatas(identifyCode, year, month, energyTypeNo);
                return query.ExecuteDataset();
            }
            //else if (energyTypeNo == "16")
            //{
            //    UQ_GetHeatDayDatas query = new UQ_GetHeatDayDatas(identifyCode, year, month, energyTypeNo);
            //    return query.ExecuteDataset();
            //}
            return null;
        }

        public DataSet GetAmmeterTimeDatas(string identifyCode, int year, int month, int day, string energyTypeNo = "01")
        {
            if (energyTypeNo == "01")
            {
                UQ_GetAmmeterTimeDatas query = new UQ_GetAmmeterTimeDatas(identifyCode, year, month, day, energyTypeNo);
                return query.ExecuteDataset();
            }
            //else if (energyTypeNo == "02")
            //{
            //    UQ_GetWaterTimeDatas query = new UQ_GetWaterTimeDatas(identifyCode, year, month, day, energyTypeNo);
            //    return query.ExecuteDataset();
            //}
            else if (energyTypeNo == "03")
            {
                UQ_GetGasTimeDatas query = new UQ_GetGasTimeDatas(identifyCode, year, month, day, energyTypeNo);
                return query.ExecuteDataset();
            }
            //else if (energyTypeNo == "16")
            //{
            //    UQ_GetHeatTimeDatas query = new UQ_GetHeatTimeDatas(identifyCode, year, month, day, energyTypeNo);
            //    return query.ExecuteDataset();
            //}
            return null;
        }

        public DataSet GetAmmeterRankByPeople(int year, int month)
        {
            UQ_GetAmmeterRankByPeople query = new UQ_GetAmmeterRankByPeople(year, month);
            return query.ExecuteDataset();
        }

        public DataSet GetWaterRankByPeople(int year, int month)
        {
            UQ_GetWaterRankByPeople query = new UQ_GetWaterRankByPeople(year, month);
            return query.ExecuteDataset();
        }

        public DataSet GetAmmeterRankByBuildingArea(int year, int month)
        {
            UQ_GetAmmeterRankByBuildingArea query = new UQ_GetAmmeterRankByBuildingArea(year, month);
            return query.ExecuteDataset();
        }

        public DataSet ListAmmerterWaterGasByXun(int year, int month, string identifyCode)
        {
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            dt.Columns.Add("ReadingMonth", typeof(string));
            dt.Columns.Add("AmmeterMonthData", typeof(double));
            dt.Columns.Add("AmmeterCoal", typeof(double));
            dt.Columns.Add("AmmeterMoney", typeof(double));
            dt.Columns.Add("WaterMonthData",typeof(double));
            dt.Columns.Add("WaterMoney", typeof(double));
            dt.Columns.Add("GasMonthData", typeof(double));
            dt.Columns.Add("GasCoal", typeof(double));
            dt.Columns.Add("GasMoney", typeof(double));
            UQ_ListAmmerterByXun ammeterQuery = new UQ_ListAmmerterByXun(year, month, identifyCode);
            DataSet ammeterDs = ammeterQuery.ExecuteDataset();
            UQ_ListWaterByXun waterQuery = new UQ_ListWaterByXun(year, month, identifyCode);
            DataSet waterDs = waterQuery.ExecuteDataset();
            UQ_ListGasByXun gasQuery = new UQ_ListGasByXun(year, month, identifyCode);
            DataSet gasDs = gasQuery.ExecuteDataset();

            for (int i = 0; i < 3; i++)
            {
                DataRow dr = dt.NewRow();
                dr = GetNewDataRow(ammeterDs, waterDs, gasDs, dr, i);
                dt.Rows.Add(dr);
            }
            ds.Tables.Add(dt);
            return ds;
            //DataSet ds = query.ExecuteDataset();
            //if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
            //{
            //    DataRow dr1 = ds.Tables[0].NewRow();
            //    dr1 = AddDataRow(1, dr1);
            //    ds.Tables[0].Rows.Add(dr1);
            //    DataRow dr2 = ds.Tables[0].NewRow();
            //    dr2 = AddDataRow(2, dr2);
            //    ds.Tables[0].Rows.Add(dr2);
            //    DataRow dr3 = ds.Tables[0].NewRow();
            //    dr3 = AddDataRow(3, dr3);
            //    ds.Tables[0].Rows.Add(dr3);
            //}
            //if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count < 3)
            //{
            //    int count = ds.Tables[0].Rows.Count;
            //    if (count == 1) //只有上旬数据
            //    {
            //        DataRow dr2 = ds.Tables[0].NewRow();
            //        dr2 = AddDataRow(2, dr2);
            //        ds.Tables[0].Rows.Add(dr2);
            //        DataRow dr3 = ds.Tables[0].NewRow();
            //        dr3 = AddDataRow(3, dr3);
            //        ds.Tables[0].Rows.Add(dr3);
            //    }
            //    else if (count == 2)//只有上旬和中旬数据
            //    {
            //        DataRow dr3 = ds.Tables[0].NewRow();
            //        dr3 = AddDataRow(3, dr3);
            //        ds.Tables[0].Rows.Add(dr3);
            //    }
            //}
            //return ds;
        }

        private DataRow  GetNewDataRow(DataSet ammeterDs, DataSet waterDs, DataSet gasDs, DataRow dr,int i)
        {
            string ch = string.Empty;
            if (i == 0)
            {
                dr["ReadingMonth"] = "上旬";
                ch = "A";
            }
            else if(i==1)
            {
                dr["ReadingMonth"] = "中旬";
                ch = "B";
            }
            else 
            {
                dr["ReadingMonth"] = "下旬";
                ch = "C";
            }

            //电 
            if (ammeterDs == null || ammeterDs.Tables.Count == 0 || ammeterDs.Tables[0].Rows.Count == 0)
            {
                dr["AmmeterMonthData"] = 0;
                dr["AmmeterCoal"] = 0;
                dr["AmmeterMoney"] = 0;
            }
            else
            {
                bool isAmmeterExits = false;
                foreach (DataRow ammeterDr in ammeterDs.Tables[0].Rows)
                {
                    if (ammeterDr["ReadingMonth"].ToString() == ch)
                    {
                        dr["AmmeterMonthData"] = ammeterDr["AmmeterMonthData"];
                        dr["AmmeterCoal"] = ammeterDr["AmmeterCoal"];
                        dr["AmmeterMoney"] = ammeterDr["AmmeterMoney"];
                        isAmmeterExits = true;
                        break;
                    }
                }
                if (!isAmmeterExits)
                {
                    dr["AmmeterMonthData"] = 0;
                    dr["AmmeterCoal"] = 0;
                    dr["AmmeterMoney"] = 0;
                }
            }

            //水
            if (waterDs == null || waterDs.Tables.Count == 0 || waterDs.Tables[0].Rows.Count == 0)
            {
                dr["WaterMonthData"] = 0;
                dr["WaterMoney"] = 0;
            }
            else
            {
                bool isWaterExist = false;
                foreach (DataRow waterDr in waterDs.Tables[0].Rows)
                {
                    if (waterDr["ReadingMonth"].ToString() == ch)
                    {
                        dr["WaterMonthData"] = waterDr["WaterMonthData"];
                        dr["WaterMoney"] = waterDr["WaterMoney"];
                        isWaterExist = true;
                        break;
                    }
                }
                if (!isWaterExist)
                {
                    dr["WaterMonthData"] = 0;
                    dr["WaterMoney"] = 0;
                }
            }

            //气
            if (gasDs == null || gasDs.Tables.Count == 0 || gasDs.Tables[0].Rows.Count == 0)
            {
                dr["GasMonthData"] = 0;
                dr["GasCoal"] = 0;
                dr["GasMoney"] = 0;
            }
            else
            {
                bool isGasExist = false;
                foreach (DataRow gasDr in gasDs.Tables[0].Rows)
                {
                    if (gasDr["ReadingMonth"].ToString() == "A")
                    {
                        dr["GasMonthData"] = gasDr["GasMonthData"];
                        dr["GasCoal"] = gasDr["GasCoal"];
                        dr["GasMoney"] = gasDr["GasMoney"];
                        isGasExist = true;
                        break;
                    }
                }
                if (!isGasExist)
                {
                    dr["GasMonthData"] = 0;
                    dr["GasCoal"] = 0;
                    dr["GasMoney"] = 0;
                }
            }
            return dr;
        }

        public DataRow AddDataRow(int type, DataRow dr)
        {
            if (type == 1)
                dr[0] = "上旬";
            else if (type == 2)
                dr[0] = "中旬";
            else
                dr[0] = "下旬";

            dr[1] = 0;
            dr[2] = 0;
            dr[3] = 0;
            dr[4] = 0;
            dr[5] = 0;
            dr[6] = 0;
            dr[7] = 0;
            dr[8] = 0;
            return dr;
        }

        public DataTable GetThreeMonthWithAmmeterGas(int year, int month, string identifyCode)
        {
            DateTime todayDt = new DateTime(year, month, 1);
            DateTime oneDt = todayDt.AddMonths(-1);
            DateTime twoDt = todayDt.AddMonths(-2);

            DataTable dtable = new DataTable();
            dtable.Columns.Add("ReadingMonth", typeof(int));
            dtable.Columns.Add("AmmeterCoal", typeof(double));
            dtable.Columns.Add("GasCoal", typeof(double));
            UQ_GetThreeMonthWithAmmeterGas twoQuery = new UQ_GetThreeMonthWithAmmeterGas(twoDt.Year, twoDt.Month, identifyCode);
            DataSet twoDs = twoQuery.ExecuteDataset();
            if (twoDs != null && twoDs.Tables.Count > 0 && twoDs.Tables[0].Rows.Count > 0)
            {
                DataRow twoDr = dtable.NewRow();
                twoDr[0] = twoDt.Month;
                twoDr[1] = twoDs.Tables[0].Rows[0]["AmmeterCoal"];
                twoDr[2] = twoDs.Tables[0].Rows[0]["GasCoal"];
                dtable.Rows.Add(twoDr);
            }
            UQ_GetThreeMonthWithAmmeterGas oneQuery = new UQ_GetThreeMonthWithAmmeterGas(oneDt.Year, oneDt.Month, identifyCode);
            DataSet oneDs = oneQuery.ExecuteDataset();
            if (oneDs != null && oneDs.Tables.Count > 0 && oneDs.Tables[0].Rows.Count > 0)
            {
                DataRow oneDr = dtable.NewRow();
                oneDr[0] = oneDt.Month;
                oneDr[1] = oneDs.Tables[0].Rows[0]["AmmeterCoal"];
                oneDr[2] = oneDs.Tables[0].Rows[0]["GasCoal"];
                dtable.Rows.Add(oneDr);
            }
            UQ_GetThreeMonthWithAmmeterGas todayQuery = new UQ_GetThreeMonthWithAmmeterGas(todayDt.Year, todayDt.Month, identifyCode);
            DataSet todayDs = todayQuery.ExecuteDataset();
            if (todayDs != null && todayDs.Tables.Count > 0 && todayDs.Tables[0].Rows.Count > 0)
            {
                DataRow todayDr = dtable.NewRow();
                todayDr[0] = todayDt.Month;
                todayDr[1] = todayDs.Tables[0].Rows[0]["AmmeterCoal"];
                todayDr[2] = todayDs.Tables[0].Rows[0]["GasCoal"];
                dtable.Rows.Add(todayDr);
            }
            return dtable;
        }

        public DataSet GetCurrentMonthMoney(int year, int month, string identifyCode)
        {
            UQ_GetCurrentMonthMoney query = new UQ_GetCurrentMonthMoney(year,month,identifyCode);
            return  query.ExecuteDataset();
        }
    }
}
